home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter22 / isohex22_2 / isohex22_2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-28  |  7.5 KB  |  290 lines

  1. /*****************************************************************************
  2. IsoHex22_2.cpp
  3. Ernest S. Pazera
  4. 28NOV2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires GDICanvas.h/cpp
  7. No other libs are required
  8. *****************************************************************************/
  9.  
  10. //////////////////////////////////////////////////////////////////////////////
  11. //INCLUDES
  12. //////////////////////////////////////////////////////////////////////////////
  13. #define WIN32_LEAN_AND_MEAN  
  14.  
  15. #include <windows.h>   
  16. #include <stdlib.h>
  17. #include "gdicanvas.h"
  18.  
  19. //////////////////////////////////////////////////////////////////////////////
  20. //DEFINES
  21. //////////////////////////////////////////////////////////////////////////////
  22. //name for our window class
  23. #define WINDOWCLASS "ISOHEX22"
  24. //title of the application
  25. #define WINDOWTITLE "IsoHex 22-2"
  26.  
  27. //map constants
  28. const int MAPWIDTH=320;
  29. const int MAPHEIGHT=320;
  30. const int MAPSEEDS=100;
  31. const int LANDPERC=30;
  32.  
  33. //////////////////////////////////////////////////////////////////////////////
  34. //PROTOTYPES
  35. //////////////////////////////////////////////////////////////////////////////
  36. bool Prog_Init();//game data initalizer
  37. void Prog_Loop();//main game loop
  38. void Prog_Done();//game clean up
  39.  
  40. //////////////////////////////////////////////////////////////////////////////
  41. //GLOBALS
  42. //////////////////////////////////////////////////////////////////////////////
  43. HINSTANCE hInstMain=NULL;//main application handle
  44. HWND hWndMain=NULL;//handle to our main window
  45.  
  46. //canvas for the map
  47. CGDICanvas gdicMap;
  48.  
  49. //////////////////////////////////////////////////////////////////////////////
  50. //WINDOWPROC
  51. //////////////////////////////////////////////////////////////////////////////
  52. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  53. {
  54.     //which message did we get?
  55.     switch(uMsg)
  56.     {
  57.     case WM_DESTROY://the window is being destroyed
  58.         {
  59.  
  60.             //tell the application we are quitting
  61.             PostQuitMessage(0);
  62.  
  63.             //handled message, so return 0
  64.             return(0);
  65.  
  66.         }break;
  67.     case WM_PAINT://the window needs repainting
  68.         {
  69.             //a variable needed for painting information
  70.             PAINTSTRUCT ps;
  71.             
  72.             //start painting
  73.             HDC hdc=BeginPaint(hwnd,&ps);
  74.  
  75.             //end painting
  76.             EndPaint(hwnd,&ps);
  77.  
  78.             //draw the map
  79.             hdc=GetDC(hWndMain);
  80.             BitBlt(hdc,0,0,MAPWIDTH,MAPHEIGHT,gdicMap,0,0,SRCCOPY);
  81.             ReleaseDC(hWndMain,hdc);
  82.                                     
  83.             //handled message, so return 0
  84.             return(0);
  85.         }break;
  86.     }
  87.  
  88.     //pass along any other message to default message handler
  89.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  90. }
  91.  
  92.  
  93. //////////////////////////////////////////////////////////////////////////////
  94. //WINMAIN
  95. //////////////////////////////////////////////////////////////////////////////
  96. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  97. {
  98.     //assign instance to global variable
  99.     hInstMain=hInstance;
  100.  
  101.     //create window class
  102.     WNDCLASSEX wcx;
  103.  
  104.     //set the size of the structure
  105.     wcx.cbSize=sizeof(WNDCLASSEX);
  106.  
  107.     //class style
  108.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  109.  
  110.     //window procedure
  111.     wcx.lpfnWndProc=TheWindowProc;
  112.  
  113.     //class extra
  114.     wcx.cbClsExtra=0;
  115.  
  116.     //window extra
  117.     wcx.cbWndExtra=0;
  118.  
  119.     //application handle
  120.     wcx.hInstance=hInstMain;
  121.  
  122.     //icon
  123.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  124.  
  125.     //cursor
  126.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  127.  
  128.     //background color
  129.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  130.  
  131.     //menu
  132.     wcx.lpszMenuName=NULL;
  133.  
  134.     //class name
  135.     wcx.lpszClassName=WINDOWCLASS;
  136.  
  137.     //small icon
  138.     wcx.hIconSm=NULL;
  139.  
  140.     //register the window class, return 0 if not successful
  141.     if(!RegisterClassEx(&wcx)) return(0);
  142.  
  143.     //create main window
  144.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  145.  
  146.     //error check
  147.     if(!hWndMain) return(0);
  148.  
  149.     //if program initialization failed, then return with 0
  150.     if(!Prog_Init()) return(0);
  151.  
  152.     //message structure
  153.     MSG msg;
  154.  
  155.     //message pump
  156.     for(;;)    
  157.     {
  158.         //look for a message
  159.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  160.         {
  161.             //there is a message
  162.  
  163.             //check that we arent quitting
  164.             if(msg.message==WM_QUIT) break;
  165.             
  166.             //translate message
  167.             TranslateMessage(&msg);
  168.  
  169.             //dispatch message
  170.             DispatchMessage(&msg);
  171.         }
  172.  
  173.         //run main game loop
  174.         Prog_Loop();
  175.     }
  176.     
  177.     //clean up program data
  178.     Prog_Done();
  179.  
  180.     //return the wparam from the WM_QUIT message
  181.     return(msg.wParam);
  182. }
  183.  
  184. //////////////////////////////////////////////////////////////////////////////
  185. //INITIALIZATION
  186. //////////////////////////////////////////////////////////////////////////////
  187. bool Prog_Init()
  188. {
  189.     //seed the random number generator
  190.     srand(GetTickCount());
  191.  
  192.     //set the client area size
  193.     RECT rcTemp;
  194.     SetRect(&rcTemp,0,0,MAPWIDTH,MAPHEIGHT);//256x256 client area
  195.     AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
  196.     SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height
  197.  
  198.     //grab the window's dc
  199.     HDC hdc=GetDC(hWndMain);
  200.     //make the map
  201.     gdicMap.CreateBlank(hdc,MAPWIDTH,MAPHEIGHT);
  202.  
  203.     //clear out the map's canvas with blue
  204.     HBRUSH hbr=CreateSolidBrush(RGB(0,0,255));
  205.     FillRect(gdicMap,&rcTemp,hbr);
  206.     DeleteObject(hbr);
  207.  
  208.     //select a green pen into the canvas.
  209.     HPEN hpen=CreatePen(PS_SOLID,0,RGB(0,255,0));
  210.     SelectObject(gdicMap,hpen);
  211.  
  212.     int x,y,count;
  213.     //pick random location
  214.     x=rand()%MAPWIDTH;
  215.     y=rand()%MAPHEIGHT;
  216.     //move to the location
  217.     MoveToEx(gdicMap,x,y,NULL);
  218.  
  219.     //place the seeds
  220.     for(count=0;count<MAPSEEDS;count++)
  221.     {
  222.         if((rand()%4)==0)
  223.         {
  224.             //pick random location
  225.             x=rand()%MAPWIDTH;
  226.             y=rand()%MAPHEIGHT;
  227.             //move to the location
  228.             MoveToEx(gdicMap,x,y,NULL);
  229.             SetPixelV(gdicMap,x,y,RGB(0,255,0));
  230.         }
  231.         else
  232.         {
  233.             //line to that location
  234.             x=x+rand()%50-25;
  235.             y=y+rand()%50-25;
  236.             LineTo(gdicMap,x,y);
  237.         }
  238.     }
  239.  
  240.     //place the rest of the land
  241.     for(count=0;count<MAPWIDTH*MAPHEIGHT*LANDPERC/100;count++)
  242.     {
  243.         //pick a coord next to a land square
  244.         bool found=false;
  245.         do
  246.         {
  247.             //pick random place
  248.             x=rand()%MAPWIDTH;
  249.             y=rand()%MAPHEIGHT;
  250.             //ensure the area is water
  251.             if(GetPixel(gdicMap,x,y)!=RGB(0,0,255)) continue;
  252.             //ensure it is next to another land area
  253.             if(x>=0 && GetPixel(gdicMap,x-1,y)==RGB(0,255,0)) found=true;    
  254.             if(x<(MAPWIDTH-1) && GetPixel(gdicMap,x+1,y)==RGB(0,255,0)) found=true;    
  255.             if(y>=0 && GetPixel(gdicMap,x,y-1)==RGB(0,255,0)) found=true;    
  256.             if(y<(MAPHEIGHT-1) && GetPixel(gdicMap,x,y+1)==RGB(0,255,0)) found=true;    
  257.         //keep looping until you find an appropriate space
  258.         }while(!found);
  259.         //place pixel
  260.         SetPixelV(gdicMap,x,y,RGB(0,255,0));
  261.         SendMessage(hWndMain,WM_PAINT,0,0);
  262.     }
  263.     
  264.     //return the window's dc
  265.     ReleaseDC(hWndMain,hdc);
  266.  
  267.     return(true);//return success
  268. }
  269.  
  270. //////////////////////////////////////////////////////////////////////////////
  271. //CLEANUP
  272. //////////////////////////////////////////////////////////////////////////////
  273. void Prog_Done()
  274. {
  275.     //////////////////////////
  276.     //clean up code goes here
  277.     //////////////////////////
  278. }
  279.  
  280. //////////////////////////////////////////////////////////////////////////////
  281. //MAIN GAME LOOP
  282. //////////////////////////////////////////////////////////////////////////////
  283. void Prog_Loop()
  284. {
  285.     ///////////////////////////
  286.     //main game logic goes here
  287.     ///////////////////////////
  288. }
  289.  
  290.